// pages/index/index.js
const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topWeChatName : '',
    LotteryList : '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 获取顶部推荐公众号名字
    // this.getTopWechatName()
    // 获取列表
    var timer = setInterval( ()=>{
      if(app.globalData.acc){
        that.getLotteryList()
        clearInterval(timer)
      }
    } , 100)
    
    //
  },


  onshow:function(){
    this.getLotteryList()
  },

    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getLotteryList()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取顶部推荐公众号名字
  getTopWechatName:function(){
    var that = this
    wx.request({
      url: app.globalData.url + 'getTopWechatName',
      success: (res)=>{
        if(res.msg == 'ok'){
          that.setData({
            topWeChatName:res.data.data
          })
        }
      },
      fail: ()=>{
        that.getTopWechatName()
      }
    });
  },

  // 复制剪贴板
  setClip:function(){
    var that = this
    wx.setClipboardData({
      data: that.data.topWeChatName,
      success: (result)=>{
        wx.showToast({
          title: '已复制',
          icon: 'none',
          duration: 800
        })
      },
      fail: ()=>{
        wx.showToast({
          title: '复制失败',
          icon: 'none',
          duration: 1000
        })
      }
    });
  },

  // 获取抽奖列表
  getLotteryList:function(){
    var that = this

    console.log(app.globalData.acc);
    
    wx.request({
      url: app.globalData.url + 'lotterys/get_lottery_list',
      data:{
        uid:app.globalData.acc.id
      },
      method: 'POST',
      success: (res)=>{
        console.log(res);
        var data = res.data.data
        for(var i = 0 ; i < data.length ; i ++){
          // data[i].createtime = that.toDate(data[i].createtime)
          data[i].createtime = util.formatTime(new Date(data[i].createtime * 1000))
        }
        that.setData({
          LotteryList : res.data.data
        })
      },
      fail: ()=>{
        that.getLotteryList()
      }
    });
  },

})